package cn.zwz.home.controller;

import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.basics.utils.SecurityUtil;
import cn.zwz.data.entity.User;
import cn.zwz.data.utils.ZwzNullUtils;
import cn.zwz.home.entity.Performance;
import cn.zwz.home.entity.Student;
import cn.zwz.home.entity.StudentClass;
import cn.zwz.home.service.IPerformanceService;
import cn.hutool.core.util.StrUtil;
import cn.zwz.home.service.IStudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 郑为中
 */
@Slf4j
@RestController
@Api(tags = "课堂表现管理接口")
@RequestMapping("/zwz/performance")
@Transactional
public class PerformanceController {

    @Autowired
    private IPerformanceService iPerformanceService;

    @Autowired
    private IStudentService iStudentService;

    @Autowired
    private SecurityUtil securityUtil;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条课堂表现")
    public Result<Performance> get(@RequestParam String id){
        return new ResultUtil<Performance>().setData(iPerformanceService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部课堂表现个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iPerformanceService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部课堂表现")
    public Result<List<Performance>> getAll(){
        return new ResultUtil<List<Performance>>().setData(iPerformanceService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询课堂表现")
    public Result<IPage<Performance>> getByPage(@ModelAttribute Performance performance ,@ModelAttribute PageVo page){
        QueryWrapper<Performance> qw = new QueryWrapper<>();
        if(!ZwzNullUtils.isNull(performance.getStudentId())) {
            qw.eq("student_id",performance.getStudentId());
        }
        if(!ZwzNullUtils.isNull(performance.getTitle())) {
            qw.like("title",performance.getTitle());
        }
        IPage<Performance> data = iPerformanceService.page(PageUtil.initMpPage(page),qw);
        for (Performance per : data.getRecords()) {
            if(!ZwzNullUtils.isNull(per.getStudentId())) {
                Student stu = iStudentService.getById(per.getStudentId());
                if(stu != null) {
                    per.setStudentName(stu.getUserName());
                }
            }
        }
        return new ResultUtil<IPage<Performance>>().setData(data);
    }

    @RequestMapping(value = "/getByMyPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询课堂表现")
    public Result<IPage<Performance>> getByMyPage(@ModelAttribute Performance performance ,@ModelAttribute PageVo page){
        User currUser = securityUtil.getCurrUser();
        QueryWrapper<Performance> qw = new QueryWrapper<>();
        qw.eq("student_id",currUser.getStudentId());
        if(!ZwzNullUtils.isNull(performance.getTitle())) {
            qw.like("title",performance.getTitle());
        }
        IPage<Performance> data = iPerformanceService.page(PageUtil.initMpPage(page),qw);
        for (Performance per : data.getRecords()) {
            if(!ZwzNullUtils.isNull(per.getStudentId())) {
                Student stu = iStudentService.getById(per.getStudentId());
                if(stu != null) {
                    per.setStudentName(stu.getUserName());
                }
            }
        }
        return new ResultUtil<IPage<Performance>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改课堂表现")
    public Result<Performance> saveOrUpdate(Performance performance){
        if(iPerformanceService.saveOrUpdate(performance)){
            return new ResultUtil<Performance>().setData(performance);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增课堂表现")
    public Result<Performance> insert(Performance performance) {
        if(ZwzNullUtils.isNull(performance.getStudentId())) {
            return ResultUtil.error("学生不能为空");
        }
        Student stu = iStudentService.getById(performance.getStudentId());
        if(stu == null) {
            return ResultUtil.error("学生不存在");
        }
        iPerformanceService.saveOrUpdate(performance);
        return new ResultUtil<Performance>().setData(performance);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑课堂表现")
    public Result<Performance> update(Performance performance) {
        if(ZwzNullUtils.isNull(performance.getStudentId())) {
            return ResultUtil.error("学生不能为空");
        }
        Student stu = iStudentService.getById(performance.getStudentId());
        if(stu == null) {
            return ResultUtil.error("学生不存在");
        }
        iPerformanceService.saveOrUpdate(performance);
        return new ResultUtil<Performance>().setData(performance);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除课堂表现")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iPerformanceService.removeById(id);
        }
        return ResultUtil.success();
    }
}
